package com.micro.core.configuration;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.micro.plugins.datasource.register.DynamicDataSourceRegister;
import com.micro.plugins.jwt.configuration.JwtAuthConfiguration;
import com.micro.plugins.jwt.interceptor.UserAuthRestInterceptor;
import com.micro.plugins.jwt.load.JwtLoadConfiguration;
import com.micro.plugins.jwt.runner.JwtAuthRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.spring.web.SpringfoxWebMvcConfiguration;

import java.util.List;

/**
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2019/6/11
 */
@Configuration("configWebConfig")
@Primary
@ConditionalOnClass(SpringfoxWebMvcConfiguration.class)
public class WebConfiguration implements WebMvcConfigurer {


    /**
     * 重写新增拦截器
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 用户拦截器注册
        registry.addInterceptor(getUserAuthRestInterceptor()).addPathPatterns("/**").excludePathPatterns(getJwtLoadConfiguration().getMicroJwt().getIgnoreUrlList().toArray(new String[]{}));
        //授权拦截器
//        registry.addInterceptor(getPermissionInterceptor()).addPathPatterns("/**").excludePathPatterns(getJwtLoadConfiguration().getMicroJwt().getIgnoreUrlList().toArray(new String[]{}));

    }


    /**
     * <h2>将jwt配置信息注册到spring中</h2>
     * @return
     */
    @Bean("JwtLoadConfiguration")
    JwtLoadConfiguration getJwtLoadConfiguration(){
        return new JwtLoadConfiguration();
    }

    /**
     * <h2>将jwt授权定时注册到spring中</h2>
     * @param jwtLoadConfiguration
     * @return
     */
    @Bean("JwtAuthRunner")
    JwtAuthRunner getJwtAuthRunner(JwtLoadConfiguration jwtLoadConfiguration){
        return new JwtAuthRunner(jwtLoadConfiguration);
    }

    /**
     * <h2>将jwt授权信息注册到spring中</h2>
     * @param jwtLoadConfiguration
     * @return
     */
    @Bean("JwtAuthConfiguration")
    JwtAuthConfiguration getJwtAuthConfiguration(JwtLoadConfiguration jwtLoadConfiguration){
        return new JwtAuthConfiguration(jwtLoadConfiguration);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Bean
    UserAuthRestInterceptor getUserAuthRestInterceptor() {
        return new UserAuthRestInterceptor();
    }


    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = jackson2HttpMessageConverter.getObjectMapper();
        //不显示为null的字段
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);

        jackson2HttpMessageConverter.setObjectMapper(objectMapper);
        //放到第一个
        converters.add(0, jackson2HttpMessageConverter);
    }
}